package com.zjy.portdemo.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class GPIO {
    private static BufferedReader reader = null;
    public static String getGpioString(String path) {
        String defString = "0";// 默认值
        try {
            if (reader == null){
                reader = new BufferedReader(new FileReader(path));
            }
            defString = reader.readLine();
            reader.close();
            reader = null;
        } catch (IOException e) {
//            e.printStackTrace();
            //Log.d("sss","GPIO:"+e.getMessage());
        }
        return defString;
    }

    public static boolean RootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
        return true;
    }

}
